/**
 * 
 */
package com.walle.springinpractice.ch01.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.walle.springinpractice.ch01.dao.AccountDao;
import com.walle.springinpractice.ch01.model.Account;

/**
 * @author <a href="mailto:wallellen@hotmail.com">Walle</a>
 * 
 *         2014年1月23日
 */
public class AccountService {
	private AccountDao accountDao;

	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}

	public List<Account> findDeliquentAll() throws Exception {
		List<Account> accounts = accountDao.listAll();
		List<Account> deliquentAccount = new ArrayList<Account>();
		Date date = daysAgo(30);

		for (Account account : accounts) {
			if (account.getBalance().compareTo(BigDecimal.ZERO) >= 0
					&& account.getLastPaidOn().compareTo(date) < 0) {
				deliquentAccount.add(account);
			}
		}

		return deliquentAccount;
	}

	private Date daysAgo(int day) {
		Calendar calendar = Calendar.getInstance();

		calendar.add(Calendar.DATE, -day);

		return calendar.getTime();
	}
}
